<?php


namespace App\Http\Controllers\user;


use App\Engine\BaseController;
use App\Engine\TokenService;
use App\Exceptions\ApiException;
use App\Jobs\CouponMessage;
use App\Models\CouponLog;
use App\Models\CouponRule;
use App\Models\Sys;
use Illuminate\Support\Facades\Log;
use function app\Utils\go;
use App\Models\CouponMessage as CouponMessageModel;

class Coupon extends BaseController
{

    public function get_coupon_price()
    {
        $res=CouponMessageModel::where(["uid"=>TokenService::getCurrentUid(),"state"=>0])->sum("price");
        CouponMessageModel::where(["uid"=>TokenService::getCurrentUid(),"state"=>0])->update(["state"=>1]);
        return go(["data"=>$res]);
    }

    public function add_coupon()
    {
        $uid=$this->get["invite_uid"];
        $coupon_on=Sys::getValue("coupon_on");
        $coupon_type=Sys::getValue("coupon_type");
   /*     if ($coupon_on==0||$coupon_type==0){
            return go(["data"=>true]);
        }*/
        $day=1;
        $price=0;

      /*  $yesterdayLogs=CouponLog::where("uid",$uid)->orderBy("id","desc")->first();
        if(!$yesterdayLogs){
            $rule=CouponRule::first();
            if (!$rule){
                return go(["data"=>"false"]);
            }else{
                $price=$rule["price"];
            }
        }else{
            $rule=CouponRule::where("day",$yesterdayLogs["day"]+1)->first();
            $day=$yesterdayLogs["day"]+1;
                if ($rule){
                $price=$rule["price"];
            }
        }*/
        $price=Sys::getValue("coupon_price");
        $is=CouponLog::where(["uid"=>TokenService::getCurrentUid(),"date"=>date("Y-m-d"),"invite_uid"=>$this->get["invite_uid"]])->first();
        if ($is){
            return go(["data"=>true]);
        }
        if($price>0){
            $create=[
                "uid"=>TokenService::getCurrentUid(),
                "invite_uid"=>$this->get["invite_uid"],
                "date"=>date("Y-m-d"),
                "day"=>$day,
                "price"=>$price
            ];
            $res=CouponLog::create($create);

            if ($this->get["invite_uid"]>0){
                $send=[
                    "invite_uid"=>$this->get["invite_uid"],
                    "price"=>$price
                ];
                CouponMessage::dispatch($send);
            }

        }





        return go(["data"=>"ok"]);
    }



    /**判断用户是否第一次登录
     * @return array
     */
    public function check_coupon()
    {
       // $coupon=CouponLog::where("uid",TokenService::getCurrentUid())->count();
        $flat=true;
      /*  if ($coupon==0){
            $flat=true;
        }*/

        return go(["data"=>$flat]);
    }

    /**
     * 用户第一次登录领取优惠卷
     */
    public function get_first_coupon()
    {
        $coupon=CouponLog::where("uid",TokenService::getCurrentUid())->count();
       /* if($coupon>0)
            throw new ApiException("操作失败");*/
        $price=Sys::getValue("first_coupon");
        if(!$price){
            $price=0;
        }
        $create=[
            "uid"=>TokenService::getCurrentUid(),
            "date"=>date("Y-m-d"),
            "day"=>1,
            "price"=>$price,
            "invite_uid"=>0
        ];
        $res=CouponLog::create($create);
        return go($res);
    }
    //
}
